package com.yc.vote.biz;

import com.yc.vote.entity.TpUser;
import com.yc.vote.mapper.TpUserMapper;
import com.yc.vote.util.MBHelper;
import org.apache.ibatis.session.SqlSession;

public class TpUserBiz {

    public TpUser login(String uname, String pwd) throws BizException {
        if(uname==null || uname.trim().isEmpty()){
            throw new BizException("请填写用户名");
        }
        if(pwd==null || pwd.trim().isEmpty()){
            throw new BizException("请填写密码");
        }
        try(SqlSession session = MBHelper.openSession()){
            TpUserMapper m = session.getMapper(TpUserMapper.class);
            TpUser user = m.selectForLogin(uname, pwd);
            if(user == null){
                throw new BizException("用户名或密码错误");
            }
            return user;
        } catch (RuntimeException e){
            // 通知管理员 => 发邮件, 发短信
            throw new BizException("业务繁忙,请稍后再试",e);
        }
    }
}
